﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using NDK.Module.DAL;
using NDK.Module.Model;
using NDK.LocalUtils;
using NDK.Utils;

namespace NDK.Frm
{
    public partial class Dlg_AddUser : Form
    {
        private OpaqueLayer m_OpaqueLayer = null;
        private static log4net.ILog logger = null;
        //采用委托传值到settingForm
        public delegate void PutHandler(string sizeName, string sizeProperty, string normValue, string upperDeviation, string lowerDeviation);
        public PutHandler putSizeTextHandler;
        SysUserServices sysUserService = new SysUserServices();
        private string _title;

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public Dlg_AddUser()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            int x = SystemInformation.PrimaryMonitorSize.Width / 2 - this.Width / 2;
            int y = SystemInformation.PrimaryMonitorSize.Height / 2 - this.Height / 2;
            this.Location = new Point(x, y);
        }

        private void Dlg_AddSize_Load(object sender, EventArgs e)
        {
            logger = log4net.LogManager.GetLogger(this.GetType());

            lbl_Title.Text = _title;
            
            if (_title == "添加用户")
            {
                tb_UserName.Show();
                cb_UserName.Hide();
            }
            else
            {
                tb_UserName.Hide();
                cb_UserName.Show();
                lbl_Ok.Text = "确认";
                List<string> ls = sysUserService.GetAllUsername();
                cb_UserName.DataSource = ls;
            }
            OpaqueLayerUtils.ShowOpaqueLayer(this.Owner, out m_OpaqueLayer, 170, false);
            AnimateWindow(this.Handle, 200, Convert.ToInt32(WindowsEffect.AW_BLEND));
            StyleUtils.DrowRoundedForm(this, 25, 0.1);
        }
        #region 引入窗体动画效果方法
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags);
        #endregion

        private void lbl_Ok_Click(object sender, EventArgs e)
        {
            AccessControlUtils user = new AccessControlUtils();
            user.Username = Common.username;
            user.Password = Common.password;
            if (CheckInput() == true)
            {
                    if (_title == "添加用户")
                    {
                        if (user.IsAuthorized(UserAuthority.添加用户,tb_Password.Text))
                        {
                            SysUserModel model = new SysUserModel();
                            model.Username = tb_UserName.Text.Trim();
                            model.Password = tb_Password.Text.Trim();
                            model.LoginMode = (LoginMode)(0);

                            IEnumerator enumerator = lba_Authority.CheckedItems.GetEnumerator();
                            string authorities = "";
                            while (enumerator.MoveNext())
                            {
                                authorities += (enumerator.Current.ToString() + ",");
                            }
                            model.Authority = authorities;
                            sysUserService.Add(model);

                            OpaqueLayerUtils.HideOpaqueLayer(m_OpaqueLayer);
                            this.DialogResult = DialogResult.OK;
                        }
                        else
                        {
                            lbl_Tip.Text = "权限不足！";
                        }
                    }
                    if (_title == "编辑用户") 
                    {
                        
                    }
                    if (_title == "删除用户") 
                    {
                        user.Username = Common.username;
                        user.Password = Common.password;
                        if (user.IsAuthorized(UserAuthority.删除用户,tb_Password.Text))
                        {
                            sysUserService.Delete(cb_UserName.Text);
                            OpaqueLayerUtils.HideOpaqueLayer(m_OpaqueLayer);
                            this.DialogResult = DialogResult.OK;
                        }
                        else 
                        {
                            lbl_Tip.Text = "权限不足！";
                        }
                    }
                    
                else
                {
                    lbl_Tip.ForeColor = Color.Red;
                    lbl_Tip.Text = "密码错误！";
                }

            }
        }

        private bool CheckInput()
        {
            if (tb_UserName.Text != string.Empty && tb_Password.Text != string.Empty)
            {
                return true;
            }
            else 
            {
                lbl_Tip.Text = "用户名和密码不能为空！";
                return false;
            }
            
        }

        #region UI
        private void lbl_Ok_MouseEnter(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Ok, true);
        }

        private void lbl_Ok_MouseLeave(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Ok, false);
        }

        private void lbl_Cancel_Click(object sender, EventArgs e)
        {
            OpaqueLayerUtils.HideOpaqueLayer(m_OpaqueLayer);
            this.DialogResult = DialogResult.Cancel;
        }

        private void lbl_Cancel_MouseEnter(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Cancel, true);
        }

        private void lbl_Cancel_MouseLeave(object sender, EventArgs e)
        {
            StyleUtils.SetControlSelectedStyle(lbl_Cancel, false);
        }

        private void Dlg_AddSize_Paint(object sender, PaintEventArgs e)
        {
            StyleUtils.DrowRoundedForm(this, 25, 0.1);
        } 
        #endregion

        private void cb_UserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SysUserModel model = sysUserService.GetModel(cb_UserName.Text);
            tb_UserName.Text = model.Username;

            foreach (ListBoxItem item in lba_Authority.Items)
            {
                item.CheckState = CheckState.Unchecked;
            }
            string[] authorities = model.Authority.Split(',');
            foreach (string s in authorities)
            {
                foreach (ListBoxItem item in lba_Authority.Items)
                {
                    if (item.Text == s)
                    {
                        item.CheckState = CheckState.Checked;
                    }
                }
            }
        }

    }
}
